package com.example.a2032.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.amap.api.fence.GeoFence;
import com.example.a2032.MainActivity;
import com.example.a2032.R;
import com.example.a2032.Utils.BleSdk;
import com.example.a2032.Utils.GeoFenceUtils;
import com.example.a2032.Utils.LocationUtils;
import com.example.a2032.Utils.MoniDataUtils;
import com.example.a2032.Utils.SchedulersTransformer;
import com.example.a2032.Utils.SerialPortUtil;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.Utils.Utils;
import com.example.a2032.Utils.WebSocketUtils;
import com.example.a2032.Utils.WifiConnectUtils;
import com.example.a2032.fragment.TopFragment;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class BindTipsActivity extends BaseActivity {

    @BindView(R.id.iv_tips)
    ImageView ivTips;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    static final int Tips_NoSim = 0;
    static final int Tips_NoNet = 1;
    int tipsType;
    private static final String KEY_TipsType = "TipsType";
    private TelephonyManager telephonyManager;
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_tips);
        ButterKnife.bind(this);
        init();

    }

    void init() {
        tipsType = getIntent().getIntExtra(KEY_TipsType, Tips_NoNet);
        tvTips.setText(tipsType == Tips_NoSim ? getString(R.string.tips_sim) : getString(R.string.tips_net));
        ivTips.setImageResource(tipsType == Tips_NoSim ? R.drawable.tips_sim : R.drawable.tips_net);
        String userName = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.watchId);
        if (!TextUtils.isEmpty(userName)) {
            startActivity(MainActivity.class);
            finish();
            return;
        }
        subscribe(this);
        registerReceiver();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myReceiver != null)
            unregisterReceiver(myReceiver);
        unSubscribe(disposableTimer);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    private void registerReceiver() {
        telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        myReceiver = new MyReceiver();
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.intent.action.SIM_STATE_CHANGED");
        registerReceiver(myReceiver, mIntentFilter);
    }

    private static final String TAG = "BindTipsActivity";

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "android.intent.action.SIM_STATE_CHANGED":
                    int simState = telephonyManager.getSimState();
                    Log.i(TAG, "onReceive: SIM_STATE_CHANGED " + simState);
                    switch (simState) {
                        case TelephonyManager.SIM_STATE_READY:
                            startConnectTimer();
                            break;
                        case TelephonyManager.SIM_STATE_UNKNOWN:
                        case TelephonyManager.SIM_STATE_ABSENT:
                        case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                        case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                        case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                        default:
                            break;
                    }
                    break;


            }
        }

    }

    Disposable disposableTimer;

    public void startConnectTimer() {
        if (disposableTimer != null && !disposableTimer.isDisposed()) return;
        Observable.interval(0, 3, TimeUnit.SECONDS).compose(SchedulersTransformer.<Long>io2MainObservable()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposableTimer = d;
            }

            @Override
            public void onNext(Long aLong) {
                Log.i(TAG, "onNext: ");
              //  SerialPortUtil.getInstance().restartSerialPort(BleSdk.getDeviceMac());
                  WebSocketUtils.getInstance().toConnect();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }

    ;
}